// 写一个程序创建进程链（即父子孙三代进程）
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc,char*argv[])
{
    pid_t cpid;
    pid_t spid;
    cpid=fork();
    if(cpid < 0)
    {
        printf("error");
        return 1;
    }
    if(cpid > 0)
    {
        printf("In parent process,pid = %d\n",getpid());
        wait(NULL);
    }
    else
    {//cpid == 0
        spid = fork();
        if(spid < 0)
        {
            printf("error");
            return 1;
        }
        if(spid > 0)
        {//child process
            printf("In child process,pid = %d\n",getpid());
            return 0;
        }
        else
        {//grandson process
            printf("In grandson process,pid = %d\n",getpid());
            printf("doing something ....\n");
            sleep(6);
            return 0;
        }
    }
    return 0;
}